// 单例模式创建缓存
class LocalStorageSingleTon {
  private constructor() {}
  private static instance: LocalStorageSingleTon | null
  // 命名空间
  private namespace: string = 'project'
  // 是否创建实例
  static getInstance() {
    if (this.instance == null) {
      this.instance = new LocalStorageSingleTon()
    }
    return this.instance
  }
  // 根据key设置浏览器存储
  setItem(key: string, val: string) {
    const namespacedKey: string = this._getNameSpaceKey(key)
    localStorage.setItem(namespacedKey, val)
  }
  // 根据key获取浏览器存储
  getItem(key: string) {
    const namespacedKey: string = this._getNameSpaceKey(key)
    return localStorage.getItem(namespacedKey)
  }
  // 根据key移除浏览器存储
  removeItem(key: string) {
    const namespacedKey: string = this._getNameSpaceKey(key)
    localStorage.removeItem(namespacedKey)
  }
  // 清除所有缓存
  clear() {
    localStorage.clear()
  }
  private _getNameSpaceKey(key: string): string {
    return `${this.namespace}_${key}`
  }
}

const localStorageInstance = LocalStorageSingleTon.getInstance()

export default localStorageInstance
